package com.bnz.bnzshop.service.impl;

import com.bnz.bnzshop.dao.*;
import com.bnz.bnzshop.entity.GoodsDescEntity;
import com.bnz.bnzshop.entity.GoodsEntity;
import com.bnz.bnzshop.entity.ItemEntity;
import com.bnz.bnzshop.service.GoodsService;
import com.bnz.bnzshop.vo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsDescDao goodsDescDao;
    @Autowired
    private ItemCatDao itemCatDao;
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private SellerDao sellerDao;
    @Autowired
    private ItemDao itemDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void save(Goods goods) {
        goods.getGoods().setSellerId("baidu");
        this.save(goods.getGoods());

        goods.getDesc().setGoodsId(goods.getGoods().getId());
        goodsDescDao.insert(goods.getDesc());

        goods.getTbItems().stream().forEach(v -> {
            v.setGoodsId(goods.getGoods().getId());
            v.setCategoryid(goods.getGoods().getCategory3Id());
            String category = itemCatDao.selectById(goods.getGoods().getCategory3Id()).getName();
            v.setCategory(category);
            String brand = brandDao.selectById(goods.getGoods().getBrandId()).getName();
            v.setBrand(brand);
            String seller = sellerDao.selectById(goods.getGoods().getSellerId()).getName();
            v.setSellerId(goods.getGoods().getSellerId());
            v.setSeller(seller);
            v.setCreateTime(new Date());
            v.setUpdateTime(new Date());
            itemDao.insert(v);
        });


    }

    @Override
    public Goods selectOne(Long id) {
        GoodsEntity goods = goodsDao.selectById(id);
        GoodsDescEntity desc = goodsDescDao.selectById(id);
        List<ItemEntity> tbItems = itemDao.selectList(new QueryWrapper<ItemEntity>().eq("goods_id", id));
        return new Goods(goods,desc,tbItems);
    }

    @Override
    public void updateAll(Goods goods) {
        this.updateById(goods.getGoods());

        goodsDescDao.updateById(goods.getDesc());

        itemDao.delete(new QueryWrapper<ItemEntity>().eq("goods_id",goods.getGoods().getId()));

        goods.getTbItems().stream().forEach(v -> {
            v.setGoodsId(goods.getGoods().getId());
            v.setCategoryid(goods.getGoods().getCategory3Id());
            String category = itemCatDao.selectById(goods.getGoods().getCategory3Id()).getName();
            v.setCategory(category);
            String brand = brandDao.selectById(goods.getGoods().getBrandId()).getName();
            v.setBrand(brand);
            String seller = sellerDao.selectById(goods.getGoods().getSellerId()).getName();
            v.setSellerId(goods.getGoods().getSellerId());
            v.setSeller(seller);
            v.setCreateTime(new Date());
            v.setUpdateTime(new Date());
            itemDao.insert(v);
        });
    }
}